﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Infrastructure.HardCoding;
using LinQProject;
using ServiceProject.EntityService;

public partial class MasterPage : System.Web.UI.MasterPage
{
    protected FarmacieDataClassesDataContext database
    {
        get
        {
            if (Session["database"] == null)
                Session["database"] = new FarmacieDataClassesDataContext();
            return (FarmacieDataClassesDataContext)Session["database"];
        }
    }
    
    protected void Page_Load(object sender, EventArgs e)
    {
        ucLogin.IsLogged = !String.IsNullOrEmpty(Session["username"].ToString());
        if (!IsPostBack)
        {
            LoadTree();
            BindMenu();
        }
        Page.Title = "Farmacia Iedera";
        //selCos.Selection = (IDictionary<string, SelectedProduct>)Session["SelectionForCos"];
        //selCos.LoadRepeater();
    }

    private void BindMenu()
    {
        IList<LinQProject.Menu> listaMenu = new MenuService(database).GetAllActiveMenu();
        foreach (var menu in listaMenu)
        {
            menuID.Items.Add(new MenuItem(menu.MenuName, menu.MenuId.ToString(), "", menu.MenuPage, ""));
        }  
    }

    private void LoadTree()
    {
        TreeViewMeniuMS.Nodes.Clear();
        CategoryService categService = new CategoryService(database);
        ProductCategoryService prodCategService=new ProductCategoryService(database);

        List<Category> listaCategory = (List<Category>)categService.GetAllActiveCategory();
        foreach (Category c in listaCategory)
        {
            TreeNode tnm = new TreeNode(c.categoryName, c.categoryId.ToString());
            TreeViewMeniuMS.Nodes.Add(tnm);

            List<ProductCategory> listaProductCategory = (List<ProductCategory>)prodCategService.GetAllActiveCategoryByCategory(c.categoryId);
            foreach (ProductCategory pc in listaProductCategory)
            {
                TreeNode tng = new TreeNode(pc.ProductCategoryName, pc.ProductCategoryId.ToString());
                tnm.ChildNodes.Add(tng);
            } //end foreach grupe
        }//end foreach module
    }

    protected void TreeViewMeniuMS_SelectedNodeChanged(object sender, EventArgs e)
    {
        TreeViewMeniuMS.SelectedNode.Expand();
        if (TreeViewMeniuMS.SelectedNode.Depth == (int)Enums.enumNodeDepth.lastNode)
        {
            int productCategoryId = Convert.ToInt32(TreeViewMeniuMS.SelectedNode.Value);
            string DestinationUrl = String.Format("~/Products.aspx?categoryId={0}", productCategoryId);
            Response.Redirect(DestinationUrl);
        }
    }
}
